package com.yanyu.space.common.minio;

import com.yanyu.space.common.core.bootlog.BootstrapLogContext;
import com.yanyu.space.common.core.properties.MinioProperties;
import com.yanyu.space.common.minio.support.CustomMinioClient;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yanyu
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(value = MinioProperties.class)
public class MinioAutoConfig {

    @Autowired
    private MinioProperties minioProperties;

    @Bean
    public CustomMinioClient customMinioClient() {
        MinioClient minioClient;
        try {
            minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getEndpoint())
                    .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                    .build();
        } catch (Exception e) {
            log.error("初始化 Minio 客户端失败：" + e.getMessage());
            throw e;
        }
        BootstrapLogContext.put("minio", String.format("开启mino功能, 连接地址 -> %s", minioProperties.getEndpoint()));
        return new CustomMinioClient(minioClient);
    }
}
